package com.afd.ozblip.activity;

import java.util.List;

import com.afd.ozblip.entity.CategoryEntity;
import com.afd.ozblip.util.Constants;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MoreClassActivity extends BaseActivity {

	/****************** 视图变量 **************************/
	private RadioGroup categoryFilterLeftLayout;
	private FrameLayout categoryFilterRightLayout;
	/****************** 数据变量 **************************/
	private int classLen;
	private int parentClassIndex;
	private List<CategoryEntity> categoryList;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.more_class);
		// 启动返回功能
		toBack();
		// 寻找视图
		findViews();
		// 设置监听
		setListeners();
		// 获取参数
		getBundle();
		// 初始化分类
		initCategory();
	}

	/**
	 * 寻找视图
	 */
	public void findViews() {
	}

	/**
	 * 设置监听
	 */
	public void setListeners() {
		View.OnClickListener moreClick = new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.ll_more_layout_setting:
					Intent settingIntent = new Intent(MoreClassActivity.this,
							SettingActivity.class);
					startActivity(settingIntent);
					break;

				default:
					break;
				}
			}
		};
	}

	/**
	 * 获取传递的参数
	 */
	private void getBundle() {
		//Intent intent = getIntent();
	}

	// TODO 初始化分类
	private void initCategory() {
		this.categoryFilterLeftLayout = (RadioGroup) findViewById(R.id.ll_conditon_filter_category_left);
		this.categoryFilterRightLayout = (FrameLayout) findViewById(R.id.ll_conditon_filter_category_right);

		LayoutInflater inflater = LayoutInflater.from(this);
		categoryList = Constants.getCategoryList();
		classLen = categoryList.size();
		View.OnClickListener leftClick = new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				int index = Integer.parseInt(v.getTag().toString());
				parentClassIndex = index;
				for (int i = 0, len = classLen; i < len; i++) {
					categoryFilterRightLayout.getChildAt(i).setVisibility(8);
				}
				categoryFilterRightLayout.getChildAt(index).setVisibility(0);
			}
		};
		View.OnClickListener rightClick = new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				int index = Integer.parseInt(v.getTag().toString());
				String parentClassName = categoryList.get(parentClassIndex)
						.getParentClassName();
				if("热门分类".equals(parentClassName)){
					parentClassName = "";
				}
				String childClassName = categoryList.get(parentClassIndex)
						.getChildClassList().get(index);
				Intent kafeiIntent = new Intent(MoreClassActivity.this,
						NearbyBusinessActivity.class);
				kafeiIntent.putExtra("parentClassName", parentClassName);
				kafeiIntent.putExtra("childClassName", childClassName);
				startActivity(kafeiIntent);
			}
		};
		for (int i = 0, len = categoryList.size(); i < len; i++) {
			View leftView = inflater.inflate(R.layout.category_left_item, null);
			RadioButton leftRB = (RadioButton) leftView;
			String parentCN = categoryList.get(i).getParentClassName();
			leftRB.setText(parentCN);
			leftRB.setId(i);
			if ("热门分类".equals(parentCN)) {
				leftRB.setChecked(true);
			}
			// 设置drawableleft
			BitmapDrawable leftbd = getListIco(i);
			leftRB.setCompoundDrawablesWithIntrinsicBounds(leftbd, null, null,
					null);
			// 设置tag
			leftRB.setTag(i);
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.MATCH_PARENT,
					LinearLayout.LayoutParams.WRAP_CONTENT);
			leftView.setOnClickListener(leftClick);
			this.categoryFilterLeftLayout.addView(leftView, lp);
			LinearLayout ll = new LinearLayout(this);
			ll.setLayoutParams(new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.MATCH_PARENT,
					LinearLayout.LayoutParams.MATCH_PARENT));
			ll.setOrientation(LinearLayout.VERTICAL);
			ll.setVisibility(i == 0 ? 0 : 8);
			List<String> childClassList = categoryList.get(i)
					.getChildClassList();
			for (int j = 0, childLen = childClassList.size(); j < childLen; j++) {
				View rightView = inflater.inflate(R.layout.category_right_item,
						null);
				rightView.setBackgroundResource(R.drawable.ll_bg_selector);
				rightView.setTag(j);
				rightView.setOnClickListener(rightClick);
				TextView rightTv = (TextView) rightView
						.findViewById(R.id.tv_category_right);
				rightTv.setText(childClassList.get(j));

				ll.addView(rightView, lp);
			}
			this.categoryFilterRightLayout.addView(ll);
		}
	}
	private BitmapDrawable getListIco(int index) {
		Resources res = getResources();
		BitmapDrawable bd = null;
		Bitmap bm = null;
		switch (index) {
		case 0:
			bm = BitmapFactory.decodeResource(res, R.drawable.list_ico1);
			bd = new BitmapDrawable(res, bm);
			break;
		case 1:
			bm = BitmapFactory.decodeResource(res, R.drawable.list_ico2);
			bd = new BitmapDrawable(bm);
			break;
		case 2:
			bm = BitmapFactory.decodeResource(res, R.drawable.list_ico3);
			bd = new BitmapDrawable(bm);
			break;
		case 3:
			bm = BitmapFactory.decodeResource(res, R.drawable.list_ico4);
			bd = new BitmapDrawable(bm);
			break;
		case 4:
			bm = BitmapFactory.decodeResource(res, R.drawable.list_ico5);
			bd = new BitmapDrawable(bm);
			break;
		case 5:
			bm = BitmapFactory.decodeResource(res, R.drawable.list_ico6);
			bd = new BitmapDrawable(bm);
			break;
		case 6:
			bm = BitmapFactory.decodeResource(res, R.drawable.list_ico7);
			bd = new BitmapDrawable(bm);
			break;
		case 7:
			bm = BitmapFactory.decodeResource(res, R.drawable.list_ico8);
			bd = new BitmapDrawable(bm);
			break;

		default:
			break;
		}
		return bd;
	}

}